package com.quiz.view;

import java.util.ArrayList;

import com.quiz.R;
import com.quiz.TopicCoverActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;

public class CustomAdapter extends BaseAdapter {
	private Context mContext;
	private ArrayList<String> mItems;
	Button  btn;
	
	public CustomAdapter(Context c, ArrayList<String> items) {
		this.mContext = c;
		this.mItems = items;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mItems.size();
	}
	@Override
	public Object getItem(int index) { 
		// TODO Auto-generated method stub
		return mItems.get(index);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int topicID = position + 1;
		View view;
		if (convertView == null) {
			btn = new Button(mContext);
			Drawable icon = null;
			btn.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			LayoutInflater li = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = li.inflate(R.layout.grid_item, null);
			btn = (Button)view.findViewById(R.id.gridItem_btn);
			switch (position) {
			case 0:
				btn.setId(position);
				icon = mContext.getResources().getDrawable(R.drawable.topic_java_jp1);
				btn.setText(mContext.getResources().getText(R.string.topic_java_basic));
				break;
			case 1:
				icon = mContext.getResources().getDrawable(R.drawable.topic_java_jp2);
				btn.setText(mContext.getResources().getText(R.string.topic_java_advanced)); 
				btn.setId(position);
				break;   
			case 2:
				icon = mContext.getResources().getDrawable(R.drawable.topic_java_xml);
				btn.setText(mContext.getResources().getText(R.string.topic_java_xml));
				btn.setId(position);  
				break;
			case 3:
				icon = mContext.getResources().getDrawable(R.drawable.topic_java_jsp);
				btn.setText(mContext.getResources().getText(R.string.topic_jsp_servlet));  
				btn.setId(position); 
				break;
			case 4:
				icon = mContext.getResources().getDrawable(R.drawable.topic_java_jsf);
				btn.setText(mContext.getResources().getText(R.string.topic_java_server_faces));
				btn.setId(position); 
				break;
			case 5:
				icon = mContext.getResources().getDrawable(R.drawable.topic_java_jws); 
				btn.setText(mContext.getResources().getText(R.string.topic_java_webservice));
				btn.setId(position); 
				break;
			case 6:
				icon = mContext.getResources().getDrawable(R.drawable.topic_java_ejb);
				btn.setText(mContext.getResources().getText(R.string.topic_ejb));
				btn.setId(position); 
				break;
			}
			btn.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
			btn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext,TopicCoverActivity.class);
					intent.putExtra("topic_id", topicID);
					mContext.startActivity(intent);
				}
			});
		}else{
			view = convertView; 
		}
		return view;
	}

}
